| Conditions | 11 |
| Paths | 22 |
| Total Lines | 61 |
| Lines | 38 |
| Ratio | 62.3 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like canvas.js ➔ prepareCanvas often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import merge from "../help/merge.js"; |
||
| 24 | this.moveCanvasDrawing(this.encodings[i]); |
||
| 25 | } |
||
| 26 | |||
| 27 | this.restoreCanvas(); |
||
| 28 | } |
||
| 29 | |||
| 30 | prepareCanvas(){ |
||
| 31 | // Get the canvas context |
||
| 32 | var ctx = this.canvas.getContext("2d"); |
||
| 33 | |||
| 34 | ctx.save(); |
||
| 35 | |||
| 36 | calculateEncodingAttributes(this.encodings, this.options, ctx); |
||
| 37 | var totalWidth = getTotalWidthOfEncodings(this.encodings); |
||
| 38 | var maxHeight = getMaximumHeightOfEncodings(this.encodings); |
||
| 39 | |||
| 40 | this.canvas.width = totalWidth + this.options.marginLeft + this.options.marginRight; |
||
| 41 | |||
| 42 | this.canvas.height = maxHeight; |
||
| 43 | |||
| 44 | // Paint the canvas |
||
| 45 | ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); |
||
| 46 | if(this.options.background){ |
||
| 47 | ctx.fillStyle = this.options.background; |
||
| 48 | ctx.fillRect(0, 0, this.canvas.width, this.canvas.height); |
||
| 49 | } |
||
| 50 | |||
| 51 | ctx.translate(this.options.marginLeft, 0); |
||
| 52 | } |
||
| 53 | |||
| 54 | drawCanvasBarcode(options, encoding){ |
||
| 55 | // Get the canvas context |
||
| 56 | var ctx = this.canvas.getContext("2d"); |
||
| 57 | |||
| 58 | var binary = encoding.data; |
||
| 59 | |||
| 60 | // Creates the barcode out of the encoded binary |
||
| 61 | var yFrom; |
||
| 62 | if(options.textPosition == "top"){ |
||
| 63 | yFrom = options.marginTop + options.fontSize + options.textMargin; |
||
| 64 | } |
||
| 65 | else{ |
||
| 66 | yFrom = options.marginTop; |
||
| 67 | } |
||
| 68 | |||
| 69 | ctx.fillStyle = options.lineColor; |
||
| 70 | |||
| 71 | for(var b = 0; b < binary.length; b++){ |
||
| 72 | var x = b * options.width + encoding.barcodePadding; |
||
| 73 | |||
| 74 | if(binary[b] === "1"){ |
||
| 75 | ctx.fillRect(x, yFrom, options.width, options.height); |
||
| 76 | } |
||
| 77 | else if(binary[b]){ |
||
| 78 | ctx.fillRect(x, yFrom, options.width, options.height * binary[b]); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | drawCanvasText(options, encoding){ |
||
| 84 | // Get the canvas context |
||
| 85 | var ctx = this.canvas.getContext("2d"); |
||
| 138 |